package org.hna.eking.AutoTest;

import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Set;

import javax.imageio.ImageIO;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.internal.WrapsDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class TestFirst {
	String BaseUrl = "";
	WebDriver Browserdriver = null;
	String WrkPath;

	public TestFirst() {
		WrkPath = System.getProperty("user.dir");

		String DirPath = WrkPath + "\\bin\\chromedriver.exe";
		System.out.println(DirPath);
		// System.out.println(DirPath);
		// DesiredCapabilities des = DesiredCapabilities.firefox();
		// des.setCapability("webdriver.gecko.driver",
		// "D:\\git\\AutoTest\\bin\\geckodriver.exe");
		// System.setProperty("webdriver.edge.bin","D:\\Program Files\\Mozilla
		// Firefox\\firefox.exe");
		System.setProperty("webdriver.chrome.driver", DirPath);

		Browserdriver = new ChromeDriver();

		// File pathToFirefoxBinary = new File("D:\\Program Files\\Mozilla
		// Firefox\\firefox.exe");
		// FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary);
		// Browserdriver = new FirefoxDriver(firefoxbin,null);
	}

	public TestFirst(String ParmUrl) {
		Browserdriver = new FirefoxDriver();
		BaseUrl = ParmUrl;
	}

	public Boolean test() throws Exception {

		Browserdriver.get("https://cas.jdair.net/");
		Thread.sleep(2);
		Browserdriver.manage().window().maximize();
		// Browserdriver.get("https://cas.jdair.net/captcha.htm");
		// Actions act = new Actions(Browserdriver);
		// act.sendKeys(Keys.CONTROL + Keys.valueOf("T"));
		// act.click().build().perform();

		// Browserdriver.navigate().back();
		WebElement img = Browserdriver.findElement(By.id("jcaptcha"));
		// WebElement img = Browserdriver.findElement(By.id("btn_enter_id"));
		// Point loc = img.getLocation();
		// img.getSize();
		WrapsDriver wrapsDriver = (WrapsDriver) img;

		File screenshot = ((TakesScreenshot) wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.FILE);
		Rectangle rectangle = new Rectangle(img.getLocation(), img.getSize());
		// (img.getLocation().x, img.getLocation().y, img.getSize());
		Point location = img.getLocation();
		BufferedImage bufferedImage = ImageIO.read(screenshot);
		BufferedImage destImage = bufferedImage.getSubimage(location.x, location.y, rectangle.width, rectangle.height);
		ImageIO.write(destImage, "png", screenshot);

		String FileSrcPath = WrkPath + "\\tmp\\";
		String FileSrcName = "scr" + String.valueOf(Thread.currentThread().getId()) + ".png";

		FileUtils.copyFile(screenshot, new File(FileSrcPath + FileSrcName));
		ImageOcr ocr = new ImageOcr();
		String AutCode = ocr.ImageOcrRun(FileSrcPath, FileSrcName);
		Browserdriver.findElement(By.id("use_name")).clear();
		Browserdriver.findElement(By.id("use_name")).sendKeys("18500193053");
		Browserdriver.findElement(By.id("use_psd")).clear();
		Browserdriver.findElement(By.id("use_psd")).sendKeys("123456");
		Browserdriver.findElement(By.id("login_auth_code")).clear();
		Browserdriver.findElement(By.id("login_auth_code")).sendKeys(AutCode);

		Browserdriver.findElement(By.id("btn_enter_id")).click();

		if (Browserdriver.getCurrentUrl() == "www.jdair.net") {
			return true;

		}

		String ErrorMsg = Browserdriver.findElement(By.id("login_errorMessage")).getText();
		System.out.println(ErrorMsg);
		if (ErrorMsg.equals("验证码有误") || ErrorMsg.equals("必须输入验证码")) {

			for (int i = 0; i < 3; i++) {
				img = Browserdriver.findElement(By.id("jcaptcha"));
				screenshot = ((TakesScreenshot) wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.FILE);
				rectangle = new Rectangle(img.getLocation(), img.getSize());
				// (img.getLocation().x, img.getLocation().y, img.getSize());
				location = img.getLocation();
				bufferedImage = ImageIO.read(screenshot);
				destImage = bufferedImage.getSubimage(location.x, location.y, rectangle.width, rectangle.height);
				ImageIO.write(destImage, "png", screenshot);

				FileSrcPath = WrkPath + "\\tmp\\";
				FileSrcName = "scr" + String.valueOf(Thread.currentThread().getId()) + ".png";

				FileUtils.copyFile(screenshot, new File(FileSrcPath + FileSrcName));

				AutCode = ocr.ImageOcrRun(FileSrcPath, FileSrcName);

				Browserdriver.findElement(By.id("login_auth_code")).clear();
				Browserdriver.findElement(By.id("login_auth_code")).sendKeys(AutCode);

				Browserdriver.findElement(By.id("btn_enter_id")).click();
				
				if (Browserdriver.getCurrentUrl() == "www.jdair.net") {
					return true;

				}

				ErrorMsg = Browserdriver.findElement(By.id("login_errorMessage")).getText();
				if (ErrorMsg.equals("验证码有误") || ErrorMsg.equals("必须输入验证码")) {
					continue;
				}
			}
			return false;
		} else {
			return false;
		}
	}

}
